package com.atguigu.serviceedu.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.feign.OrderFeignService;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.vo.ChapterVo;
import com.atguigu.servicebase.vo.CourseInfoForm;
import com.atguigu.serviceedu.vo.front.CourseQueryVo;
import com.atguigu.serviceedu.vo.front.CourseWebVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2021-12-19
 */
@RestController
@RequestMapping("/serviceedu/front/course")
@Api(description = "前台课程列表")
//@CrossOrigin //跨域
public class CourseWebController {

    @Autowired
    EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderFeignService orderFeignService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "getCourseByPage/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "getCourseInfo/{courseId}")
    public R getCourseInfoByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideo(courseId);

        //远程调用，判断课程是否被购买
        boolean buyCourse = orderFeignService.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVoList).data("isbuy",buyCourse);
    }

    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CourseInfoForm getCourseInfoDto(@PathVariable String courseId) {
    CourseInfoForm courseInfoForm = courseService.getCourseInfo(courseId);
    return courseInfoForm;
    }

    //根据课程id查询教师名字
    @GetMapping("getTeacherNameByCourseId/{courseId}")
    public String getTeacherNameByCourseId(@PathVariable String courseId){
        String teacherName = courseService.getTeacherNameByCourseId(courseId);
        return teacherName;
    }
}

